<template>
  <el-dialog
    append-to-body
    width="600px"
    title="发布特情"
    :visible.sync="dialogValue"
    @close="emitClose"
  >
    <el-form ref="formRef" :model="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="发布特情类型">
        <el-radio-group v-model="ruleForm.releaseScenarioType">
          <el-radio label="0">公共特情</el-radio>
          <el-radio label="1">单方特情</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="ruleForm.releaseScenarioType == 1" label="接收方">
        <el-checkbox-group v-model="ruleForm.teamId">
          <el-checkbox v-for="item in participants" :label="item.id" :key="item.id">{{ item.name }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="特情名称" prop="scenarioName">
        <el-input v-model="ruleForm.scenarioName"></el-input>
      </el-form-item>
      <el-form-item label="特情详情" prop="scenarioDesc">
        <el-input type="textarea" v-model="ruleForm.scenarioDesc"></el-input>
      </el-form-item>
      <el-form-item label="想定文件">
        <UploadFile
          v-if="dialogValue"
          list-type="none"
          :list="ruleForm.scenarioFiles"
          ref="fileRef1"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="emitClose">取 消</el-button>
      <el-button type="primary" @click="emitSave">保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { drillSettingAPI, drillDetailAPI } from "@/api";
import UploadFile from "@/components/upload-file";
export default {
  components: {
    UploadFile,
  },
  props: ["id"],
  data() {
    return {
      condition: {
        maneuverId: this.$route.params.id,
      },
      request: {
        publish: drillDetailAPI.publishTeqing,
      },
      ruleForm: {
        maneuverId: "",
        releaseScenarioType: "0",
        teamId: [],
        scenarioName: "",
        scenarioDesc: "",
        scenarioFiles: [],
      },
      rules: {
        scenarioName: [{ required: true, message: "特情名称不能为空" }],
      },
      participants: [],
      dialogValue: true,
    };
  },
  created() {
    this.ruleForm = {
      maneuverId: this.$route.params.id,
      releaseScenarioType: "0",
      teamId: [],
      scenarioName: "",
      scenarioDesc: "",
      scenarioFiles: [],
    };
    drillSettingAPI.read(this.$route.params.id).then((res) => {
      this.participants = (res ? res.participants || [] : []).map((el) => {
        return {
          id: el.teamId,
          name: el.teamName,
        };
      });
    });
  },
  methods: {
    emitSave() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          // 文件上传对象
          const fileUploadForm1 = this.$refs.fileRef1.getList();
          this.request
            .publish({
              ...this.ruleForm,
              teamId:
                this.ruleForm.releaseScenarioType === "1"
                  ? this.ruleForm.teamId.join(",")
                  : "",
              scenarioFiles: fileUploadForm1.addList,
            })
            .then((res) => {
              this.$emit("save");
            });
        }
      });
    },
    emitClose() {
      this.$emit("close");
    },
  },
};
</script>

<style>
</style>
